package jwebtest.rpc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.TreeMap;

public class NodeSelect {
	public static void main(String[] args) {
		List<Node> nodeList = init();
		List<Node> caclList = new ArrayList<Node>();
		Map<String, Integer> map = new TreeMap<String, Integer>();
		for (Node n : nodeList) {
			map.put(n.getName() + "_" + n.getWeight(), n.getPriority());
		}
		// map排序
		map = sortMap(map);

		Object[] mapKeyArray = map.keySet().toArray();

		// 获取极大值的优先级
		String firstMaxPrioityKey = mapKeyArray[0].toString();
		Integer firstMaxPrioityValue = map.get(firstMaxPrioityKey);

		for (Object key : mapKeyArray) {
			Integer paramValue = map.get(key);

			if (paramValue == firstMaxPrioityValue) {
				Node t = new Node();
				t.setName(key.toString());
				t.setPriority(paramValue);

				caclList.add(t);
			} else {
				break;
			}
		}
		// 打印被选取的高优先级节点
		for (Node n : caclList) {
			System.out.println(n);
		}
	}

	public static List<Node> init() {
		// 构建list，存放1000条数据
		List<Node> nodeList = new ArrayList<Node>(1000);

		Random random = new Random(System.nanoTime());
		for (int i = 0; i < 1000; i++) {
			Node node = new Node();
			node.setName(i + "");
			node.setPriority(random.nextInt(100));
			node.setWeight(random.nextInt(100));
			nodeList.add(node);
		}
		return nodeList;
	}

	public static Map<String, Integer> sortMap(Map<String, Integer> oldMap) {
		ArrayList<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(oldMap.entrySet());
		Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {

			@Override
			public int compare(Entry<java.lang.String, Integer> arg0, Entry<java.lang.String, Integer> arg1) {
				return arg1.getValue() - arg0.getValue();
			}
		});
		Map<String, Integer> newMap = new LinkedHashMap<String, Integer>();
		for (int i = 0; i < list.size(); i++) {
			newMap.put(list.get(i).getKey(), list.get(i).getValue());
		}
		return newMap;
	}
}

/** node class */
class Node {
	private String name;
	private Integer priority;
	private Integer weight;
	private Double rate;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getPriority() {
		return priority;
	}

	public void setPriority(Integer priority) {
		this.priority = priority;
	}

	public Integer getWeight() {
		return weight;
	}

	public void setWeight(Integer weight) {
		this.weight = weight;
	}

	public String toString() {
		StringBuffer temp = new StringBuffer();
		temp.append("name:");
		temp.append(name);

		temp.append(" priority:");
		temp.append(priority);

		return temp.toString();
	}

	public Double getRate() {
		return rate;
	}

	public void setRate(Double rate) {
		this.rate = rate;
	}
}